﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MAF.Common;
using Domain.Rules;

namespace Domain.Entities
{
    /// <summary>
    /// 本类是内容管理系统的核心对象-----内容对象
    /// </summary>
    [Serializable]
    public class Content : IValidatableRegardingPersistence
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        internal Content(Guid id, string title, string text, List<Image> images, List<Attachment> attachments)
        {
            // 设定默认值
            this.Id = (Guid.Empty == id) ? CombGuid.NewGuid() : id;
            this.Title = (null == title) ? string.Empty : title;
            this.Text = (null == text) ? string.Empty : text;
            this.Images = (null == images) ? new List<Image>() : images;
            this.Attachments = (null == attachments) ? new List<Attachment>() : attachments;

            // 安装持久化规则
            _SetUpPersistenceRelatedRules();
        }

        /// <summary>
        /// 标识符
        /// </summary>
        public Guid Id { get; private set; }

        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 内容
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// 图片
        /// </summary>
        public List<Image> Images { get; set; }

        /// <summary>
        /// 附件
        /// </summary>
        public List<Attachment> Attachments { get; private set; }

        /// <summary>
        /// 建立持久化规则检查
        /// </summary>
        public void _SetUpPersistenceRelatedRules()
        {
            _persistenceRelatedRules.Add(new DateIsInRangeRule(DateTime.MinValue, DateTime.Today, "OrderDate", this));
            _persistenceRelatedRules.Add(new MaxStringLengthRule(300, "Text", this));
        }

        /// <summary>
        /// 检查是否可持久化
        /// </summary>
        /// <param name="brokenRules"></param>
        /// <returns></returns>
        public bool CheckOKToPersistence(IList<IRule> brokenRules)
        {
            if (null != brokenRules)
            {
                RuleBase.CollectBrokenRules(brokenRules, _persistenceRelatedRules);
                RuleBase.CollectBrokenRules(brokenRules, _acceptedSpecificRules);
            }

            return brokenRules.Count == 0;
        }

        /// <summary>
        /// 持久化破坏规则列表
        /// </summary>
        private List<IRule> _persistenceRelatedRules = new List<IRule>();

        /// <summary>
        /// 
        /// </summary>
        private List<IRule> _acceptedSpecificRules;
    }
}